'use strict';

const Service = require('egg').Service;

class DocService extends Service {
    async detail(id) {
        try {
            const result = await this.app.mysql.select('doc', {
                where: {
                    id
                }
            })
            
            return {
                code: 1,
                data: result.length > 0 ? result[0] : ''
            }
        }catch(e) {
            return {
                code: 0,
                msg: e.message
            }
        }
    }
    async delete(data) {
        try {
            await this.app.mysql.delete('doc', {id: data.id})
            return {
                code: 1,
                msg: '删除成功'
            }
        }catch(e) {
            return {
                code: 0,
                msg: e.message
            }
        }
    }
    async list(phone) {
        try {
            // 获取所有的文档
            const result = await this.app.mysql.select('doc', {
                where: {
                    phone
                }
            })
            return {
                code: 1,
                data: result
            }
        } catch (e) {
            return {
                code: 0,
                msg: e.message
            }
        }
    }
    async add(data) {
        try {
            // 进行数据库的插入操作

            data = Object.assign({}, data, {createTime: new Date().getTime()})

            await this.app.mysql.insert('doc', data)
            return {
                code: 1,
                msg: '添加成功'
            }

        } catch(e) {
            console.log(e.message)
            return {
                code:0,
                msg: e.message
            }
        }
    }
}

module.exports = DocService;
